const Koa = require('koa');
const Router = require('koa-router');

const mongoTest = require('./mongo-test');

const { test } = mongoTest;

const app = new Koa();
const router = new Router();

(async function register() {
    let res = await test.find({}, '-_id').lean();
    console.log(res);

    for (let row of res) {
        let { route, func } = row;
        console.log(row, route, func);
        let fun = new Function(`return function ${route.replace('/', '')} (ctx, next) { ${func} }`);
        console.log(fun.toString());
        router.get(route, fun());
    }
    app.use(router.routes()).use(router.allowedMethods());

    const port = 3000;
    app.listen(port, err => {
        if (err) {
            console.error(err);
        }
        else {
            console.info(`server start, listening ${port}...`);
        }
    });
})();